getRequestPath = function(path) {
	return cxpath + path;
}

getArgNames = function(func) {
	// 先用正则匹配,取得符合参数模式的字符串.
	// 第一个分组是这个: ([^)]*) 非右括号的任意字符
	var args = func.toString().match(/function\s*\(([^)]*)\)/)[1];
	// 用逗号来分隔参数(arguments string).
	return args.split(",").map(function(arg) {
		// 去除注释(inline comments)以及空格
		return arg.replace(/\/\*.*\*\//, "").trim();
	}).filter(function(arg) {
		// 确保没有 undefined.
		return arg;
	});
}

ajaxUtils = {};

//通过json格式发送请求
ajaxUtils.postByJson = function(url, data, callback) {
	$.ajax({
		type: "post",
		url: url,
		data: JSON.stringify(data),
		contentType: "application/json; charset=utf-8",
		success: callback
	});
}

//text格式发送请求
ajaxUtils.post = function(url, data, callback) {
	$.post(url, data, callback);
}

ajaxUtils.setAsync = function(async) {
	$.ajaxSetup({
		async:async
	});
}

ajaxUtils.setErrorHandler = function(handler) {
	
}

/**
 * 执行具体的ajax请求
 */
ajaxUtils.requestExecute = function(path, method, args, jsonFlag) {
	var url = getRequestPath(path + "/" + method);
	var argNames = args.callee.argNames;
	if (!argNames) {
		argNames = getArgNames(args.callee);
		args.callee.argNames = argNames;
	}
	if (jsonFlag) {
		this.postByJson(url, args[0], args[1]);
	} else {
		var requestParams = {};
		var callback = null;
		for (var i = 0; i < argNames.length; i++) {
			if (argNames[i] == 'callback') {
				callback = args[i];
				continue;
			}
			requestParams[argNames[i]] = args[i];
		}
		this.post(url, $.param(requestParams), callback);
	}
}
